﻿/*
// CHANGE LOG
//
mod 05 sep 07 - ddb
	ver 1.03
	changed:
		changed p_someVar:MovieClip to p_someVar:Object
//
mod 16 jun 07 - ddb
	ver 1.02
	changed:
		
	added:
		toString() method
		className_str:String
		version_str:String

*/
/*
// METHOD TABLE:
//  methods
//
function Align(){
private static function getAnchorProps (anchor:Object):Array {
public static function CenterX(obj:Object, anchor:Object, isChild:Boolean):Number {
public static function CenterY (obj:Object, anchor:Object, isChild:Boolean):Number {
public static function LeftX(obj:Object, anchor:Object, isChild:Boolean):Number {
public static function RightX(obj:Object, anchor:Object, isChild:Boolean):Number {
public static function BottomY (obj:Object, anchor:Object, isChild:Boolean):Number {
public static function TopY (obj:Object, anchor:Object, isChild:Boolean):Number {
private function toString():Void{
*/
/*

This is a static class like the Math class.
You do not have to instantiate it. You do
need to declare 'import com.ddb.Align;'
on each frame of script that uses the Class

The static class Align has six methods which return a Number.
Store the return value in a variable or use it directly while
setting a movieclip property (_x, _y)

All six methods require the same 3 arguments;
	object:Object	- the mc you want to change the position of
	target:Object	- the mc you want to align the object to/against
						  to align to the Stage use _root or _level0
	isChild:Boolean		- indicates whether the object mc is a child of the
						  anchor mc (true) or the object mc is a sibling of the
						  anchor mc (false)

The six public methods are:
	Align.CenterY()
	Align.TopY()
	Align.BottomY()
	Align.CenterX()
	Align.LeftX()
	Align.RightX()

If you are seeing unexpected results, double check the isChild argument;
think of it as: the object mc 'is a child' of the anchor mc
or: the object mc is nested inside the timeline of the anchor mc


*/
//import com.utils.*;
class com.darrendb.as2.utils.Align {
	private var className_str:String;
	private var version_str:String;
	//
	var anchorHeight:Number;
	var anchorWidth:Number;
	var anchorX:Number;
	var anchorY:Number;
	var anchorProperties:Array;
	function Align(){
		className_str = "  Align";
		version_str = "v1.03";
		trace(this);
	}
	private static function getAnchorProps (anchor:Object):Array {
		//trace("getAnchorProps()");
		// fail silently if missing argument
		if (anchor == undefined) return;
		// work-around: dynamic referencing the anchor:Object argument fails if anchor is _root or _level0
		// properties do not match stage properties
		// so, branch and use Stage Object's properties
		if (anchor == _root || anchor == _level0) {
			// use Stage Object
			var anchorHeight:Number = Stage.height;
			var anchorWidth:Number = Stage.width;
			var anchorX:Number = 0;
			var anchorY:Number = 0;
		} else {
			// use anchor
			var anchorHeight:Number = anchor._height;
			var anchorWidth:Number = anchor._width;
			var anchorX:Number = anchor._x;
			var anchorY:Number = anchor._y;
		}
		var returnValue = new Array(anchorX,anchorY,anchorWidth,anchorHeight);
		//trace("  anchorX:"+returnValue[0]+newline+"  anchorY:"+returnValue[1]+newline+"  anchorWidth:"+returnValue[2]+newline+"  anchorHeight:"+returnValue[3]);
		return returnValue;
	}	
	public static function CenterX(obj:Object, anchor:Object, isChild:Boolean):Number {
		// fail if required arguments are undefined
		if (obj == undefined || anchor == undefined || isChild == undefined) return;
		//trace("CenterX()");
		// get properties of anchor
		var anchorProperties:Array = getAnchorProps(anchor);
		var anchorX:Number = anchorProperties[0];
		var anchorY:Number = anchorProperties[1];
		var anchorWidth:Number = anchorProperties[2];
		var anchorHeight:Number = anchorProperties[3];
		// calculate
		if (isChild==true) {
			var returnValue:Number = (anchorWidth / 2) - (obj._width / 2); // _x coord
		} else {
			var returnValue:Number = anchorX+((anchorWidth/2)-(obj._width/2));	// _x coord
		}
		//trace("    returnValue:"+returnValue);
		return returnValue;
	}
	public static function CenterY (obj:Object, anchor:Object, isChild:Boolean):Number {
		// fail if required arguments are undefined
		if (obj == undefined || anchor == undefined || isChild == undefined) return;
		//trace("CenterY()");
		//trace("  obj:"+obj+newline+"  anchor:"+anchor+newline+"  isChild:"+isChild);
		// get properties of anchor
		var anchorProperties:Array = getAnchorProps(anchor);
		var anchorX:Number = anchorProperties[0];
		var anchorY:Number = anchorProperties[1];
		var anchorWidth:Number = anchorProperties[2];
		var anchorHeight:Number = anchorProperties[3];
		//trace("  anchorX:"+anchorX+newline+"  anchorY:"+anchorY+newline+"  anchorWidth:"+anchorWidth+newline+"  anchorHeight:"+anchorHeight);
		// calculate
		if (isChild == true) {
			var returnValue:Number = ((anchorHeight / 2) - (obj._height / 2)); // _y coord
		} else {
			var returnValue:Number = anchorY + ((anchorHeight / 2) - (obj._height / 2)); // _y coord
		}
		//trace("    returnValue:"+returnValue);
		return returnValue;
	}
	public static function LeftX(obj:Object, anchor:Object, isChild:Boolean):Number {
		// fail if required arguments are undefined
		if (obj == undefined || anchor == undefined || isChild == undefined) return;
		// get properties of anchor
		var anchorProperties:Array = getAnchorProps(anchor);
		var anchorX:Number = anchorProperties[0];
		var anchorY:Number = anchorProperties[1];
		var anchorWidth:Number = anchorProperties[2];
		var anchorHeight:Number = anchorProperties[3];
		// calculate
		if (isChild==true) {
			var returnValue:Number = 0; // _x coord
		} else {
			var returnValue:Number = anchorX; // _x coord
		}
		//trace("    returnValue:"+returnValue);
		return returnValue;
	}
	public static function RightX(obj:Object, anchor:Object, isChild:Boolean):Number {
		// fail if required arguments are undefined
		if (obj == undefined || anchor == undefined || isChild == undefined) return;
		// get properties of anchor
		var anchorProperties:Array = getAnchorProps(anchor);
		var anchorX:Number = anchorProperties[0];
		var anchorY:Number = anchorProperties[1];
		var anchorWidth:Number = anchorProperties[2];
		var anchorHeight:Number = anchorProperties[3];
		// calculate
		if (isChild==true) {
			var returnValue:Number = (anchorWidth - obj._width); // _x coord
		} else {
			var returnValue:Number = anchorX +(anchorWidth - obj._width); // _x coord
		}
		//trace("    returnValue:"+returnValue);
		return returnValue;
	}
	public static function BottomY (obj:Object, anchor:Object, isChild:Boolean):Number {
		// fail if required arguments are undefined
		if (obj == undefined || anchor == undefined || isChild == undefined) return;
		// get properties of anchor
		var anchorProperties:Array = getAnchorProps(anchor);
		var anchorX:Number = anchorProperties[0];
		var anchorY:Number = anchorProperties[1];
		var anchorWidth:Number = anchorProperties[2];
		var anchorHeight:Number = anchorProperties[3];
		// calculate
		if (isChild==true) {
			var returnValue:Number = ((anchorHeight) - (obj._height)); // _y coord
		} else {
			var returnValue:Number = anchorY + ((anchorHeight) - (obj._height)); // _y coord
		}
		//trace("    returnValue:"+returnValue);
		return returnValue;
	}
	public static function TopY (obj:Object, anchor:Object, isChild:Boolean):Number {
		// fail if required arguments are undefined
		if (obj == undefined || anchor == undefined || isChild == undefined) return;
		// get properties of anchor
		var anchorProperties:Array = getAnchorProps(anchor);
		var anchorX:Number = anchorProperties[0];
		var anchorY:Number = anchorProperties[1];
		var anchorWidth:Number = anchorProperties[2];
		var anchorHeight:Number = anchorProperties[3];
		// calculate
		if (isChild==true) {
			var returnValue:Number = 0; // _y coord
		} else {
			var returnValue:Number = anchorY; // _y coord
		}
		//trace("    returnValue:"+returnValue);
		return returnValue;
	}
	
	private function toString():String {
		return className_str + " ("+version_str+")";
	}
}
